import java.io.*;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:进行普通文件复制
 * User: 87111
 * Date: 2025-11-12
 * Time: 15:12
 */
public class Demo8 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入源文件路径：");
        String oldPath = sc.next();
        //判断输入的是不是文件
        File oldFile = new File(oldPath);
        if(!oldFile.isFile()) {
            System.out.println("源文件不存在或者不是文件");
            return;
        }

        System.out.println("请输入要复制到的文件路径");
        String newPath = sc.next();
        //判断复制到的路径目录存不存在
        File newFile = new File(newPath);
        if(!newFile.getParentFile().isDirectory()) {
            System.out.println("该文件所在目录不存在");
            return;
        }
        //复制操作
        try(InputStream inputStream = new FileInputStream(oldFile);
            OutputStream outputStream = new FileOutputStream(newFile)) {
            while(true) {
                byte[] bytes = new byte[1024];
                int n = inputStream.read(bytes);
                if(n == -1) {
                    //此时文件读完了
                    break;
                }
                //写入数据
                outputStream.write(bytes,0,n);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
